<?php
namespace app\common\redis;

use app\common\library\CommonFun;
use app\common\model\activity\ActivityProfitModel;
use app\common\model\community\CommunityCommentModel;
use app\common\model\user\UserBaseModel;

/**
 * Created by PhpStorm.
 * User: 96151
 * Date: 2019/3/21
 * Time: 11:08
 */

class AnalystActivityRankRedis extends RedisModel
{
    /**
     * Redis配置名称
     * @var string
     */
    public $redis = 'default';

    /**
     * Redis key
     * @var string
     */
    public $week_key = 'zset:analyst_activity_rank_week_';

    public $month_key = "zset:analyst_activity_rank_month_";

    /**
     * 获取周榜redis_key
     * @param $time
     */
    public function get_week_rank_key( $time )
    {
        return $this->week_key.date( "W", $time );
    }


    /**
     * 获取月榜redis_key
     */
    public function get_month_rank_key( )
    {
        return $this->month_key.date( "n" );
    }


    /**
     * 分析师奖励权重计算
     */
    public function compute_analyst( $time = "" )
    {
        $time = $time ?: time();

        $is_month = 0;

        if ( $time > 1555862400 ){
            $is_month = 1;
        }

        // 获取现有的分析师列表
        $UserBaseModel = UserBaseModel::getInstance();

        $where = [
            "type_bat" => UserBaseModel::USER_TYPE_ANALYST
        ];

        $user_id_list = $UserBaseModel->search_id_list_no_page( $where );

        $redis_key = $is_month ?  $this->get_month_rank_key() : $this->get_week_rank_key( $time ) ;

        // 获取本周的开始时间和结束时间
        $date = $is_month ?  CommonFun::getThisMonthStartEndTime( date( "Y-m-d H:i:s", $time ) ) : CommonFun::getThisWeekStartEndTime( date( "Y-m-d H:i:s", $time ) );

        // 获取当前范围段的奖励
        $ActivityProfitModel = ActivityProfitModel::getInstance();

        $where = [
            "type" => [
                "in", [
                    // 被评论收益
//                    ActivityProfitModel::PROFIT_TYPE_COMMENT_PASSIVE,
                    // 被点赞收益
                    ActivityProfitModel::PROFIT_TYPE_UP_PASSIVE,
                    // 被分享收益
                    ActivityProfitModel::PROFIT_TYPE_SHARE_PASSIVE
                ],
            ],

            "create_time" => [
                "between",
                [
                    date( "Y-m-d", $date['start']),
                    date( "Y-m-d", $date['end']),
                ]
            ],
            "user_id"   => [
                "in",
                $user_id_list
            ]
        ];

        $profit_list = $ActivityProfitModel->field( "user_id,type,profit" )->where( $where )->select();

        // 汇总奖励系数
        $analyst_count = [];

        // 汇总奖励系数
        foreach ( $profit_list as $value ){
            if ( !$analyst_count[$value['user_id']] ){
                $analyst_count[$value['user_id']] = 0;
            }

            switch ( $value['type'] ){
                case ActivityProfitModel::PROFIT_TYPE_UP_PASSIVE :
                    $analyst_count[$value['user_id']] = bcadd( bcmul( $value['profit'], 1.5 ), $analyst_count[$value['user_id']] );
                break;

                case ActivityProfitModel::PROFIT_TYPE_SHARE_PASSIVE :
                    $analyst_count[$value['user_id']] = bcadd( bcmul( $value['profit'], 3.5 ), $analyst_count[$value['user_id']] );
                break;
            }
        }

        var_dump( $analyst_count );

        // 汇总优质评论
        $CommunityCommentModel = CommunityCommentModel::getInstance();

        $where = [
            "is_good" => 1,
            "create_time" => [
                "between",
                [
                    date( "Y-m-d", $date['start']),
                    date( "Y-m-d", $date['end']),
                ]
            ],
            "user_id" => [
                "in",
                $user_id_list
            ]
        ];

        $comment_list = $CommunityCommentModel->get_key_value( "id", "user_id", $where );

        // 计算优质评论权重
        foreach ( $comment_list as $value ){
            if ( !$analyst_count[$value] ){
                $analyst_count[$value] = 0;
            }

            $analyst_count[$value] += 500;
        }

        var_dump( $analyst_count );

        $this->Delete( $redis_key );

        $this->SortSetZmadd( $analyst_count ,$redis_key );

        echo "计算完成";
    }

    /**
     * 获取一个用户的权重
     */
    public function compute_analyst_user( $user_id, $time = "" )
    {
        $time = $time ?: time();
        $is_month = 0;

        if ( $time > 1555862400 ){
            $is_month = 1;
        }

        // 获取本周的开始时间和结束时间
        $date = $is_month ? CommonFun::getThisMonthStartEndTime( date( "Y-m-d H:i:s", $time ) ) : CommonFun::getThisWeekStartEndTime( date( "Y-m-d H:i:s", $time ) );

        // 获取当前范围段的奖励
        $ActivityProfitModel = ActivityProfitModel::getInstance();

        $where = [
            "type" => [
                "in", [
                    // 被评论收益
//                    ActivityProfitModel::PROFIT_TYPE_COMMENT_PASSIVE,
                    // 被点赞收益
                    ActivityProfitModel::PROFIT_TYPE_UP_PASSIVE,
                    // 被分享收益
                    ActivityProfitModel::PROFIT_TYPE_SHARE_PASSIVE
                ],
            ],
            "create_time" => [
                "between",
                [
                    date( "Y-m-d", $date['start']),
                    date( "Y-m-d", $date['end']),
                ]
            ],
            "user_id" => $user_id
        ];

        $profit_list = $ActivityProfitModel->field( "user_id,type,profit" )->where( $where )->select();

        $data = [
            "up_count" => 0,
//            "comment_count" => 0,
            "share_count"   => 0,
            "good_comment_count" => 0
        ];

        // 汇总奖励系数
        foreach ( $profit_list as $value ){
            switch ( $value['type'] ){
//                case ActivityProfitModel::PROFIT_TYPE_COMMENT_PASSIVE:
//                    $data['comment_count'] += (int) $value['profit'];
//                    break;
                case ActivityProfitModel::PROFIT_TYPE_UP_PASSIVE:
                    $data['up_count'] += (int) $value['profit'];
                    break;
                case ActivityProfitModel::PROFIT_TYPE_SHARE_PASSIVE:
                    $data['share_count'] += (int) $value['profit'];
                    break;
                default:
                    break;
            }
        }

        // 汇总优质评论
        $CommunityCommentModel = CommunityCommentModel::getInstance();

        $where = [
            "is_good" => 1,
            "create_time" => [
                "between",
                [
                    date( "Y-m-d", $date['start']),
                    date( "Y-m-d", $date['end']),
                ]
            ],

            "user_id" => $user_id
        ];

        $good_count = $CommunityCommentModel->where( $where )->count();
        $data['good_comment_count'] = $good_count * 100;
        return $data;
    }




}